<?php
/**
 * Manages openID url
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage @@EXTENSIONNAME@@
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

$IncludePath = @ini_get('include_path');
@ini_set('include_path', $IncludePath . PATH_SEPARATOR . OPENID_DIR . '/include');

include_once("Auth/OpenID/Consumer.php");
include_once("Auth/OpenID/FileStore.php");
include_once("Auth/OpenID/SReg.php");

/**
 * Manages openID url
 *
 * @package Extensions
 * @subpackage @@EXTENSIONNAME@@
 */
Class OpenID_OpenIdManager {

	/**
	 * @var Context
	 */
	var $Context;

	/**
	 * @var Auth_OpenID_Consumer
	 */
	var $Consumer;


	/**
	 * Constructor
	 *
	 * @param Context $Context
	 * @return OpenIdManager
	 */
	function OpenID_OpenIdManager(&$Context) {
		$this->Context =& $Context;
		$this->User =& $User;

		$Store =& new Auth_OpenID_FileStore(
			$this->Context->Configuration['OPENID_CACHE_DIR']);
		$this->Consumer =& new Auth_OpenID_Consumer($Store);
	}

	/**
	 * Request authentication
	 *
	 * @param string $Url OpenID url
	 * @param string $ReturnUrl Url the identity provider with send back the authentication info
	 */
	function RequestAuth($Url, $ReturnUrl){
		$AuthRequest = $this->Consumer->begin($Url);

		if (!$AuthRequest) {
			$this->Context->WarningCollector->Add($this->Context->GetDefinition(
				'You did not enter a valid openID'));
		} else {
			$SRegRequest = Auth_OpenID_SRegRequest::build(
				array('nickname'),array('email'));
			$AuthRequest->addExtension($SRegRequest);

			$RedirectUrl = $AuthRequest->redirectURL(
				$this->Context->Configuration['BASE_URL'],
				$ReturnUrl);

			if (Auth_OpenID::isFailure($RedirectUrl)) {
				   $this->Context->WarningCollector->Add(
					   "Could not redirect to server: " . $RedirectUrl->message);
			} else {
				Redirect($RedirectUrl);
			}
		}
	}

	/**
	 * Check if the authentication is successful and return the openId
	 *
	 * @param OpenID_User $User
	 * @return string
	 */
	function ProcessAuthResponse(&$User = false) {
		$Response = $this->Consumer->complete();

		// Check the response status.
		if ($Response->status == Auth_OpenID_CANCEL) {
			// This means the authentication was cancelled.
			$this->Context->WarningCollector->Add($this->Context->GetDefinition(
				'Verification cancelled.'));
		} else if ($Response->status == Auth_OpenID_FAILURE) {
			// Authentication failed; display the error message.
			$this->Context->WarningCollector->Add($this->Context->GetDefinition(
				'OpenID authentication failed.') . $Response->message);
		} else if ($Response->status == Auth_OpenID_SUCCESS) {
			// This means the authentication succeeded; extract the
			// identity URL and Simple Registration data (if it was
			// returned).
			$OpenId = ForceString($Response->identity_url, '');
			if ($User) {
				$User->OpenIdUrls[] = $OpenId;

				$SRegResp = Auth_OpenID_SRegResponse::fromSuccessResponse($Response);
				$Sreg = $SRegResp->contents();
				$User->Name = ForceString(@$Sreg['nickname'], '');
				$User->Email = ForceString(@$Sreg['email'], '');
			}
			return $OpenId;
		}
		return false;
	}
}